﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FastMvc.Models
{
    /// <summary>
    /// api接口返回数据的统一格式
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class BaseResult<T>
    {
        /// <summary>
        /// 接口是否调用成功
        /// </summary>
        public bool success { get; set; } = true;

        /// <summary>
        /// 接口返回错误码
        /// </summary>
        public int code { get; set; } = 0;

        /// <summary>
        /// 接口返回数据
        /// </summary>
        public T data { get; set; }

        /// <summary>
        /// 接口返回提示信息
        /// </summary>
        public string message { get; set; } = "处理成功！";

        /// <summary>
        /// 接口处理时间
        /// </summary>
        public string runtime { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        /// <summary>
        /// 错误详情（一般用于字段验证错误）
        /// </summary>
        public List<ErrorMsg> Errors { get; set; }
    }

    /// <summary>
    /// 无固定格式的返回类型（选用）
    /// </summary>
    public class BaseResult : BaseResult<dynamic>
    {
        public static BaseResult<T> Success<T>(T data, string message = "调用成功！")
        {
            return new BaseResult<T>()
            {
                code = 0,
                success = true,
                message = message,
                data = data
            };
        }

        public static BaseResult Success(string message = "调用成功！")
        {
            return new BaseResult()
            {
                code = 0,
                success = true,
                message = message,
            };
        }
    }

    /// <summary>
    /// 列表形式的数据(带total)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ListData<T>
    {
        public List<T> list { get; set; }

        public int total { get; set; }
    }

    /// <summary>
    /// 错误的具体数据
    /// </summary>
    public class ErrorMsg
    {
        /// <summary>
        /// 错误字段
        /// </summary>
        public string parameter { get; set; }

        /// <summary>
        /// 描述信息
        /// </summary>
        public string message { get; set; }
    }
}
